package com.Txunda.fxw.ui.activity.law;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.Txunda.fxw.R;
import com.Txunda.fxw.base.utils.util.JSONUtils;
import com.Txunda.fxw.base.utils.util.MapUtils;
import com.Txunda.fxw.ui.base.BaseAty;
import com.Txunda.fxw.ui.http.Office;
import com.Txunda.fxw.ui.toolkit.util.PreferenceUtil;
import com.Txunda.fxw.ui.widget.XCRoundAndOvalImageView;
import com.Txunda.fxw.view.RoundedImageView;
import com.Txunda.fxw.view.dialog.AlertDialog;
import com.bumptech.glide.Glide;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.Map;

import butterknife.BindView;

/**
 * 法律咨询
 *
 * @author Administrator
 */
public class LegalAdviceAty extends BaseAty {
    @BindView(R.id.aty_title3_tv_center)
    TextView atyTitle3TvCenter;
    @BindView(R.id.img)
    XCRoundAndOvalImageView img;
    @BindView(R.id.item_img)
    ImageView itemImg;
    @BindView(R.id.tv_area_name)
    TextView tvAreaName;
    @BindView(R.id.tv_law_name)
    TextView tvLawName;
    @BindView(R.id.tv_law_ho)
    TextView tvLawHo;
    @BindView(R.id.tv_law_wexin)
    TextView tvLawWexin;
    @BindView(R.id.tv_law_qq)
    TextView tvLawQq;
    @BindView(R.id.tv_law_phone)
    TextView tvLawPhone;
    @BindView(R.id.web_goods_at)
    WebView webGoodsAt;
    @BindView(R.id.web_law_introduce)
    WebView webLawIntroduce;
    @BindView(R.id.wpic_img)
    ImageView wpicImg;
    @BindView(R.id.tv_wpic)
    TextView tvWpic;
    @BindView(R.id.img_logo)
    RoundedImageView imgLogo;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawerLayout;
    @BindView(R.id.aty_title3_icon)
    ImageView atyTitle3Icon;
    @BindView(R.id.aty_title3_tv_right2)
    TextView atyTitle3TvRight2;
    @BindView(R.id.linear_law)
    LinearLayout linearLaw;
    @BindView(R.id.side_bar_phone_tv)
    LinearLayout sideBarPhoneTv;
    @BindView(R.id.side_bar_like_iv)
    ImageView sideBarLikeIv;
    @BindView(R.id.side_bar_like_tv)
    TextView sideBarLikeTv;
    @BindView(R.id.side_bar_like_layout)
    LinearLayout sideBarLikeLayout;
    @BindView(R.id.side_bar_evaluate_tv)
    LinearLayout sideBarEvaluateTv;
    @BindView(R.id.side_bar_settings_tv)
    LinearLayout sideBarSettingsTv;
    @BindView(R.id.side_bar_share_tv)
    LinearLayout sideBarShareTv;
    @BindView(R.id.app_side_top_layout)
    LinearLayout appSideTopLayout;
    @BindView(R.id.linear_share2)
    LinearLayout linearShare2;
    @BindView(R.id.linear_weibo)
    LinearLayout linearWeibo;
    @BindView(R.id.linear_weixin)
    LinearLayout linearWeixin;
    @BindView(R.id.linear_friends)
    LinearLayout linearFriends;
    @BindView(R.id.linear_qq)
    LinearLayout linearQq;
    @BindView(R.id.linear_bottom)
    LinearLayout linearBottom;

    private Office office;
    private String url;
    private String p_id;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WebSettings set = webGoodsAt.getSettings();
        set.setTextSize(WebSettings.TextSize.SMALLER);
        img.setType(XCRoundAndOvalImageView.TYPE_ROUND);
        img.setRoundRadius(150);
        showProgressDialog();
        office.personDetail(p_id, this);
        setDrawerLayout(drawerLayout);
        getLikesId(sideBarLikeIv, p_id);
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
        if (MapUtils.isEmpty(map))
            return;
        if (requestUrl.contains("personDetail")) {
            Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
            if (MapUtils.isEmpty(data))
                return;
            tvLawName.setText(data.get("name"));
            String wechat = data.get("wechat");
            if (wechat == null || wechat.equals("")) {
                tvLawWexin.setVisibility(View.GONE);
            } else {
                tvLawWexin.setText(data.get("wechat"));  //微信
            }
            String qq = data.get("qq");
            if (qq == null || qq.equals("")) {
                tvLawQq.setVisibility(View.GONE);
            } else {
                tvLawQq.setText(data.get("qq"));   //qq
            }
            String tel = data.get("tel");
            if (tel == null || tel.equals("")) {
                tvLawPhone.setVisibility(View.GONE);
            } else {
                tvLawPhone.setText(data.get("tel"));
            }
            String z_name = data.get("z_name");   //执业机构
            url = data.get("url");
            tvLawHo.setText(z_name);
            webGoodsAt.loadDataWithBaseURL(null, data.get("good_field"), "text/html", "utf-8", null);   //擅长领域
            webLawIntroduce.loadDataWithBaseURL(null, data.get("introduce"), "text/html", "utf-8", null);   //律师介绍

            Glide.with(this).load(data.get("w_pic")).into(wpicImg);
            tvWpic.setText(data.get("title"));
            String title = data.get("title");
            tvWpic.setText(title);
            String pic = data.get("pic");   //图片
            String d_pic = data.get("d_pic");   //地址下的底图

            Glide.with(this).load(pic).into(img);
            tvAreaName.setText(data.get("district_name"));
            Glide.with(this).load(d_pic).into(itemImg);
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_legal_advice_aty;
    }

    @Override
    @OnClick({R.id.img_logo, R.id.wpic_img, R.id.tv_law_phone, R.id.side_bar_share_tv,R.id.linear_share2})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_law_phone:    //电话
                try {
                    new AlertDialog(this).builder().setTitle("提示").setMsg("tel: " + tvLawPhone.getText().toString())
                            .setPositiveButton("确认", new OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel: " + tvLawPhone.getText().toString()));
                                    startActivity(intent);

                                }
                            }).setNegativeButton("取消", new OnClickListener() {

                        @Override
                        public void onClick(View v) {

                        }
                    }).show();

                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.wpic_img:    ///图片链接跳转
                try {
                    Uri uri = Uri.parse(url);
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                } catch (Exception e) {
                    // TODO: handle exception
                }
                break;
            case R.id.img_logo:
                if (!drawerLayout.isDrawerOpen(Gravity.END))
                    drawerLayout.openDrawer(Gravity.END);
                appSideTopLayout.setVisibility(View.VISIBLE);
                linearBottom.setVisibility(View.GONE);
                break;
            case R.id.side_bar_share_tv:
                appSideTopLayout.setVisibility(View.GONE);
                linearBottom.setVisibility(View.VISIBLE);
                break;
            case R.id.linear_share2:
                appSideTopLayout.setVisibility(View.VISIBLE);
                linearBottom.setVisibility(View.GONE);
                break;
            default:
                break;
        }
    }

    @Override
    protected void initialized() {
        p_id = getIntent().getStringExtra("p_id");
        type = "fz" + p_id;
        String logo = PreferenceUtil.readString(getApplicationContext(), PreferenceUtil.sp_logo);
        Glide.with(this).load(logo).into(imgLogo);
        office = new Office();
        atyTitle3TvCenter.setText("法律咨询");
    }

    @Override
    protected void requestData() {

    }

}
